package com.cqjtu.eecs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.eecs.entity.Course;
import com.cqjtu.eecs.entity.TeachClass;
import com.cqjtu.eecs.entity.Teacher;
import com.cqjtu.eecs.exception.CourseNotExistException;
import com.cqjtu.eecs.exception.TeacherNotExistException;
import com.cqjtu.eecs.mapper.CourseMapper;
import com.cqjtu.eecs.mapper.TeachClassMapper;
import com.cqjtu.eecs.mapper.TeacherMapper;
import com.cqjtu.eecs.service.ITeachClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 教学班 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@Service
public class TeachClassServiceImpl extends ServiceImpl<TeachClassMapper, TeachClass> implements ITeachClassService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private TeachClassMapper teachClassMapper;

    /**
     * 创建教学班并返回教学班编号
     * @param courseName 课程名
     * @param teachClassName 教学班名
     * @param teacherCode 授课教师工号
     * @return
     */
    @Override
    public Integer insertTeachClass(String courseName, String teachClassName, String teacherCode) {

        // 根据教师工号查询tid
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("teacher_code", teacherCode));
        if (teacher == null)
            throw new TeacherNotExistException("教师不存在");

        // 根据课程名查询courseId
        Course course = courseMapper.selectOne(new QueryWrapper<Course>().eq("course_name", courseName));
        if (course == null)
            throw new CourseNotExistException("课程不存在");

        // 创建教学班
        TeachClass teachClass = new TeachClass(null, course.getCourseId(), teacher.getTid(), teachClassName);
        teachClassMapper.insert(teachClass);

        // 获取教学班id
        TeachClass tc = teachClassMapper.selectOne(new QueryWrapper<TeachClass>().eq("tc_name", teachClassName));
        if (tc != null)
            return tc.getTcId();
        return null;
    }
}
